<!-- 售后详情 -->
<template>
	<view class="page_box">
		<view class="content_box">
			<!-- 步骤条 -->
			<view class="steps-box">
				<u-steps :current="stepsCurrent" :list="steps" activeColor="#39b54a" mode="number" icon="checkmark"></u-steps>
			</view>

			<!-- 服务状态 -->
			<view class="status-box u-flex u-row-between" @tap="jump('/pages/order/after-sale/log', { aftersaleId: aftersaleDetail.id })">
				<view class="">
					<view class="status-text">{{ aftersaleTitle }}</view>
					<!-- <view class="status-text">{{ aftersaleDetail.aftersale_status_desc }}</view> -->
					<view class="status-time" v-if="aftersaleDetail.latest">{{ aftersaleDetail.latest }}</view>
					<view class="status-time">{{ aftersaleDetail.updatetime }}</view>
				</view>
				<u-icon name="arrow-right" size="28" color="#666"></u-icon>
			</view>

			<!-- 退款金额 -->
			<view class="aftersale-money u-flex u-row-between">
				<view class="aftersale-money--title">退款总额</view>
				<view class="aftersale-money--num" v-if="Number(aftersaleDetail.refund_fee)">￥{{ aftersaleDetail.refund_fee }}</view>
				<view class="aftersale-money--num" v-else>未退款</view>
			</view>
			<!-- 服务商品 -->
			<view class="order-shop">
				<shopro-mini-card :title="aftersaleDetail.goods_title" :image="aftersaleDetail.goods_image">
					<template #describe>
						<view class="order-sku u-ellipsis-1">
							<text class="order-num">数量:{{ aftersaleDetail.goods_num || 0 }};</text>
							{{ aftersaleDetail.goods_sku_text ? aftersaleDetail.goods_sku_text : '' }}
						</view>
					</template>
					<template #cardBottom>
						<view class="card-price-box u-flex">
							<text class="order-price">￥{{ aftersaleDetail.goods_price || 0 }}</text>
							<button class="u-reset-button status-btn" v-if="aftersaleDetail.status_name">{{ aftersaleDetail.status_name }}</button>
						</view>
					</template>
				</shopro-mini-card>
			</view>

			<!-- 退货快递单号 -->
			<view class="goods-item u-flex u-row-between" v-if="aftersaleDetail.type=='return'&&stepsCurrent==2" @tap="showModal = true">
				<text class="item-title">寄回快递公司</text>
				<view class="u-flex refund-cause">
					<text class="u-m-r-20" style="color: #333;" v-if="express_name">{{ express_name }}</text>
					<text class="u-m-r-20" v-else>请选择快递公司</text>
					<u-icon name="arrow-right" size="28" color="#666"></u-icon>
				</view>
			</view>
			<view class="refund-item u-m-b-20" v-if="aftersaleDetail.type=='return'&&stepsCurrent==2">
				<view class="item-title">寄回快递单号</view>
				<view class="input-box u-flex">
					<input type="text" class="item-input" v-model="express_no" placeholder="请输入快递单号" placeholder-class="input--pl" />
				</view>
				<view>
					<button class="u-reset-button sub-btn" @tap="postAftersaleReturn">提交</button>
				</view>
			</view>

			<!-- 服务内容 -->
			<view class="aftersale-content">
				<view class="aftersale-item u-flex">
					<view class="item-title">服务单号：</view>
					<view class="item-content">{{ aftersaleDetail.aftersale_sn }}</view>
					<button class="u-reset-button copy-btn" @tap="onCopy(aftersaleDetail.aftersale_sn)">复制</button>
				</view>
				<view class="aftersale-item u-flex">
					<view class="item-title">申请时间：</view>
					<view class="item-content">{{ aftersaleDetail.createtime }}</view>
				</view>
				<view class="aftersale-item u-flex">
					<view class="item-title">售后类型：</view>
					<view class="item-content">{{ aftersaleDetail.type_text }}</view>
				</view>
				<view class="aftersale-item u-flex" v-if="aftersaleDetail.express">
					<view class="item-title">寄回快递：</view>
					<view class="item-content">{{ aftersaleDetail.express.express_name }}</view>
				</view>
				<view class="aftersale-item u-flex" v-if="aftersaleDetail.express">
					<view class="item-title">寄回单号：</view>
					<view class="item-content">{{ aftersaleDetail.express.express_no }}</view>
				</view>
			</view>
		</view>
		<view class="foot_box">
			<block v-for="orderBtn in aftersaleDetail.btns" :key="orderBtn">
				<button v-if="orderBtn === 'cancel'" @tap.stop="onCancel(aftersaleDetail.id)" class="u-reset-button btn">取消</button>
				<button v-if="orderBtn === 'delete'" @tap.stop="onDelete(aftersaleDetail.id)" class="u-reset-button btn delete-btn">删除</button>
			</block>
			<button class="u-reset-button contcat-btn btn" @tap="onService">联系客服</button>
		</view>

		<!-- 快递公司弹窗 -->
		<u-popup v-model="showModal" safe-area-inset-bottom @close="showModal = false" mode="bottom" :closeable="true" close-icon-pos="top-left">
			<view class="modal-box page_box">
				<view class="modal-head head_box u-flex u-row-center u-col-center">快递公司</view>
				<scroll-view class="modal-content content_box" scroll-y="true">
					<view class="u-flex-col u-p-20">
						<u-radio-group v-model="express_code" active-color="#d89f64" width="100%" @change="changeExpress">
							<u-radio class="u-m-y-10" shape="circle" v-for="item in express_list" :key="item.code" :name="item.code">
								<text class="u-p-l-20">{{ item.name }}</text>
							</u-radio>
						</u-radio-group>
					</view>
				</scroll-view>
				<view class="modal-foot foot_box u-flex u-row-center u-col-center"><button class="u-reset-button close-btn" @tap="showModal = false">确定</button></view>
			</view>
		</u-popup>
	</view>
</template>

<script>
export default {
	components: {},
	data() {
		return {
			stepsCurrent: 0,
			steps: [],
			aftersaleDetail: {}, //售后详情
			aftersaleLog: [], //售后记录
			showModal: false,
			express_name: '',
			express_code: '',
			express_no: '',
			express_list: [],
			aftersaleTitle: ''
		};
	},
	computed: {},
	onLoad() {
		this.getExpress();
		this.getAftersaleDetail();
	},
	methods: {
		jump(path, parmas) {
			this.$Router.push({
				path: path,
				query: parmas
			});
		},
		// 跳转客服
		onService() {
			//this.$Router.push('/pages/public/chat/index');
			// #ifdef APP-PLUS
			plus.runtime.openURL("https://kf.xinshuangte.cn/app/index.php?i=2&c=entry&do=index&m=mdchat_kefufree&state=index");			
			// #endif
			// #ifdef H5
			window.location.href = "https://kf.xinshuangte.cn/app/index.php?i=2&c=entry&do=index&m=mdchat_kefufree&state=index";
			// #endif
		},
		// 复制
		onCopy(code) {
			let that = this;
			uni.setClipboardData({
				data: code,
				success: function(data) {
					that.$u.toast('已复制到剪切板');
				}
			});
		},
		postAftersaleReturn() {
			let that = this
			if (that.express_code == '') {
				that.$u.toast('请选择快递公司');
				return false
			}
			if (that.express_no == '') {
				that.$u.toast('请填写快递单号');
				return false
			}
			that.$http('order.aftersaleExpress', {
				order_aftersale_id: that.$Route.query.aftersaleId,
				express_code: that.express_code,
				express_no: that.express_no
			}).then(res => {
				if (res.code == 1) {
					that.$u.toast('提交成功');
					that.getAftersaleDetail()
				}
			})
		},
		changeExpress(e) {
			let that = this
			that.express_list.forEach((item) => {
				if (item.code == e) {
					that.express_name = item.name
					return false
				}
			})
		},
		getExpress() {
			let that = this;
			that.$http('common.express').then(res => {
				if (res.code === 1) {
					that.express_list = res.data
				}
			})
		},
		// 服务单详情
		getAftersaleDetail() {
			let that = this;
			that.$http('order.aftersaleDetail', {
				id: that.$Route.query.aftersaleId
			}).then(res => {
				if (res.code === 1) {
					that.aftersaleDetail = res.data;
					that.aftersaleLog = res.data.logs;
					that.aftersaleDetail.createtime = that.$u.timeFormat(res.data.createtime, 'yyyy-mm-dd hh:MM');
					that.aftersaleDetail.updatetime = that.$u.timeFormat(res.data.updatetime, 'yyyy-mm-dd hh:MM');
					if (that.aftersaleDetail.express) {
						that.express_code = that.aftersaleDetail.express.express_code
						that.express_name = that.aftersaleDetail.express.express_name
						that.express_no = that.aftersaleDetail.express.express_no
					}
					if (that.aftersaleDetail.type == 'refund') {
						that.aftersaleTitle = that.aftersaleDetail.aftersale_status_desc
						that.steps = [
							{name: '提交申请'},
							{name: '商家处理'},
							{name: '退款成功'},
						]
						if (that.aftersaleDetail.aftersale_status == 0 || that.aftersaleDetail.aftersale_status == 1) {
							that.stepsCurrent = that.aftersaleDetail.aftersale_status;
						} else {
							that.stepsCurrent = 2;
							let name = '';
							switch (that.aftersaleDetail.aftersale_status) {
								case 3:
									name = '退款成功';
									break;
								case -1:
									name = '商家驳回';
									break;
								case -2:
									name = '用户取消';
									break;
								default:
									break;
							}
							if (name != '') {
								that.steps[2].name = name;
								that.aftersaleTitle = name
							}
						}
					}
					if (that.aftersaleDetail.type == 'return') {
						that.steps = [
							{name: '提交申请'},
							{name: '商家处理'},
							{name: '寄回商品'},
							{name: '商家退款'},
							{name: '退款成功'},
						]
						if (that.aftersaleDetail.aftersale_status >= 0 && that.aftersaleDetail.aftersale_status <= 2) {
							that.stepsCurrent = that.aftersaleDetail.aftersale_status;
						} else {
							that.stepsCurrent = 4;
							let name = '';
							switch (that.aftersaleDetail.aftersale_status) {
								case 3:
									name = '退款成功';
									break;
								case -1:
									name = '商家驳回';
									break;
								case -2:
									name = '用户取消';
									break;
								default:
									break;
							}
							if (name != '') {
								that.steps[4].name = name;
								that.aftersaleTitle = name
							}
						}
						if (that.aftersaleDetail.aftersale_status == 0) {
							that.aftersaleTitle = that.aftersaleDetail.aftersale_status_desc
						}
						if (that.aftersaleDetail.aftersale_status == 2 && that.aftersaleDetail.express && that.aftersaleDetail.express.express_no == '') {
							that.aftersaleTitle = '商家已同意退货，请按照商家给出的地址寄回'
						}
						if (that.aftersaleDetail.aftersale_status == 2 && that.aftersaleDetail.express && that.aftersaleDetail.express.express_no != '') {
							that.aftersaleTitle = '您已寄回商品，商家在收到商品后会发起退款'
							that.stepsCurrent = 3
						}
					}
				}
			});
		},
		//取消
		onCancel(aftersaleId) {
			let that = this;
			that.$http(
				'order.cancelAftersaleOrder',
				{
					id: aftersaleId
				},
				'取消中...'
			).then(res => {
				if (res.code === 1) {
					that.$Router.back();
				}
			});
		},
		// 删除
		onDelete(aftersaleId) {
			let that = this;
			uni.showModal({
				title: '删除订单',
				content: '确定要删除这个订单么？',
				cancelText: '取消',
				confirmText: '删除',
				success: res => {
					if (res.confirm) {
						that.$http('order.deleteAftersaleOrder', {
							id: aftersaleId
						}).then(res => {
							if (res.code === 1) {
								that.$Router.back();
							}
						});
					}
				}
			});
		}
	}
};
</script>

<style lang="scss">
// 步骤条
.steps-box {
	height: 190rpx;
	padding: 60rpx 0 0;
	background: #fff;
}
// 服务状态
.status-box {
	background-color: #fff;
	border-radius: 20rpx 20rpx 0px 0px;
	padding: 20rpx;
	margin-top: -20rpx;
	.status-text {
		font-size: 28rpx;

		font-weight: 500;
		color: rgba(51, 51, 51, 1);
		margin-bottom: 20rpx;
	}
	.status-time {
		font-size: 24rpx;

		font-weight: 400;
		color: rgba(153, 153, 153, 1);
	}
	.cuIcon-right {
		color: rgba(153, 153, 153, 1);
		font-size: 30rpx;
	}
}
// 退款金额
.aftersale-money {
	background-color: #fff;
	height: 98rpx;
	padding: 0 20rpx;
	margin: 20rpx 0;
	.aftersale-money--title {
		font-size: 28rpx;

		font-weight: 500;
		color: rgba(51, 51, 51, 1);
	}
	.aftersale-money--num {
		font-size: 28rpx;

		font-weight: 500;
		color: rgba(225, 33, 43, 1);
	}
}
// order-shop
.order-shop {
	padding: 20rpx;
	background-color: #fff;
	margin-bottom: 20rpx;
	.order-sku {
		font-size: 24rpx;

		font-weight: 400;
		color: rgba(153, 153, 153, 1);
		width: 450rpx;
		margin-bottom: 20rpx;
		.order-num {
			margin-right: 10rpx;
		}
	}
	.card-price-box {
		.status-btn {
			height: 32rpx;
			border: 1rpx solid rgba(207, 169, 114, 1);
			border-radius: 15rpx;
			font-size: 20rpx;
			font-weight: 400;
			color: rgba(168, 112, 13, 1);
			padding: 0 10rpx;
			margin-left: 20rpx;
			background: rgba(233, 183, 102, 0.16);
		}
		.order-price {
			font-size: 26rpx;

			font-weight: 600;
			color: rgba(51, 51, 51, 1);
		}
	}
}
// 服务内容
.aftersale-content {
	background-color: #fff;
	padding: 20rpx;
	.aftersale-item {
		height: 60rpx;
		.copy-btn {
			background: none;
			color: #b38436;
			font-size: 26rpx;
			padding: 0 20rpx;
		}
		.item-tilte {
			color: #333;
			font-size: 28rpx;
		}
		.item-content {
			color: #999999;
			font-size: 28rpx;
		}
	}
}
// 底部功能
.foot_box {
	height: 100rpx;
	background-color: #fff;
	display: flex;
	align-items: center;
	justify-content: flex-end;
	.btn {
		width: 160rpx;
		line-height: 60rpx;
		background: rgba(238, 238, 238, 1);
		border-radius: 30rpx;
		padding: 0;
		margin-right: 20rpx;
		font-size: 26rpx;

		font-weight: 400;
		color: rgba(51, 51, 51, 1);
	}
	.delete-btn {
		background: #ffeeee;
		color: #e50808;
	}
}

.refund-item {
	background-color: #fff;
	border-bottom: 1rpx solid #f5f5f5;
	padding: 30rpx;
	.item-title {
		font-size: 28rpx;
		font-weight: bold;
	}
	.input--pl {
		font-size: 24rpx;
		font-weight: 400;
		color: rgba(177, 179, 199, 1);
	}
	.input-box {
		height: 84rpx;
		background: rgba(249, 250, 251, 1);
		border-radius: 20rpx;
		.item-input {
			padding: 0 30rpx;
			flex: 1;
			font-size: 28rpx;
			font-weight: 400;
			color: #333;
		}
	}
}

.goods-item {
	height: 95rpx;
	background: #fff;
	padding: 0 25rpx;
	margin-bottom: 20rpx;

	// 售后原因
	.refund-cause {
		font-size: 26rpx;
		font-weight: 400;
		color: rgba(177, 179, 199, 1);
	}

	&:nth-of-type(2n) {
		margin-bottom: 0;
		border-top: 0;
	}

	.item-title {
		font-size: 28rpx;
		font-weight: bold;
	}

	.cuIcon-right {
		font-size: 32rpx;
	}

	.price {
		font-size: 28rpx;
		color: #a8700d;
	}
}
.modal-box {
	width: 750rpx;
	height: 680rpx;
	border-radius: 30rpx 30rpx 0 0;
	background: #fff;

	.modal-head {
		height: 100rpx;
		font-size: 30rpx;
	}

	.modal-content {
		font-size: 28rpx;

		.sel-item {
			height: 100rpx;
			width: 100%;
			padding: 0 20rpx;
			border-bottom: 1rpx solid rgba(223, 223, 223, 0.5);

			.sel-radio {
				transform: scale(0.7);
			}
		}
	}

	.modal-foot {
		.close-btn {
			width: 710rpx;
			line-height: 80rpx;
			background: linear-gradient(90deg, rgba(233, 180, 97, 1), rgba(238, 204, 137, 1));
			border: 1rpx solid rgba(238, 238, 238, 1);
			border-radius: 40rpx;
			color: rgba(#fff, 0.9);
		}
	}
}
</style>
